/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html 
 *  
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.postcodes.stripes;

import java.util.ArrayList;
import java.util.List;

import com.sfs.postcodes.beans.ConfigurationBean;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.integration.spring.SpringBean;


/**
 *
 * @author David Harrison
 * 3 May 2008
 * 
 */

public abstract class BaseActionBean implements ActionBean {

    private ActionBeanContext context;            
    private List<String> messages;    
    private ConfigurationBean configuration;
        
    
    public ActionBeanContext getContext() {
        return this.context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    protected final String getView(final String viewName) {        
        return "/WEB-INF/views/" + viewName;
    }
    
    
    protected void addMessage(String message) {
        if (this.messages == null) {
            this.messages = new ArrayList<String>();
        }
        this.messages.add(message);
    }

    protected List<String> getMessages() {
        return this.messages;
    }

    public String getMessageList() {
        StringBuffer sb = new StringBuffer();

        if (this.messages != null) {
            sb.append("<ul>");
            for (String message : this.messages) {
                sb.append("<li>");
                sb.append(message);
                sb.append("</li>");
            }
            sb.append("</ul>");
        }

        return sb.toString();
    }

    public boolean getDisplayMessages() {
        boolean displayErrors = false;
        if (getContext().getValidationErrors() != null) {
            if (getContext().getValidationErrors().size() > 0) {
                displayErrors = true;
            }
        }
        if(getMessageList().compareTo("") != 0) {
            displayErrors = true;
        }
        return displayErrors;
    }
    
    /** 
     * Setter to allow the ConfigurationBean to be injected.
     * @param configuration
     */
    @SpringBean("configuration")
    public void injectConfiguration(ConfigurationBean configuration) {
        this.configuration  = configuration;
    }
        
    public ConfigurationBean getConfiguration() {
        if(this.configuration==null) {
            this.configuration = new ConfigurationBean();
        }
        return this.configuration;
    }
    
    public String getApplicationUrl() {        
        return getConfiguration().getApplicationUrl();
    }
    
    public String getTheme() {        
        return getConfiguration().getTheme();
    }
        
    public String getGeocodeApiKey() {        
        return getConfiguration().getGeocodeApiKey();
    }
}
